package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Gate;
import com.service.GateReaderService;
import com.service.GateService;
import com.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 闸机管理业务处理类
 */
@RestController
@RequestMapping("/gate")
public class GateAction {

    @Autowired
    private GateService gateService;

    @Autowired
    private GateReaderService gateReaderService;

    /**
     * 分页获取闸机信息
     *
     * @return
     */
    @RequestMapping("/getByPage")
    public ResponseBean getByPage(String sno , @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit) {
        IPage iPage = gateService.getPager(sno, page, limit);
        return new ResponseBean(0, iPage.getTotal(), "成功", iPage.getRecords());
    }

    /**
     * 新增闸机
     */
    @RequestMapping("/addGate")
    public ResponseBean addGate(@RequestBody Gate gate) {
        gate.setPowerTime(new Date());
        if (gateService.save(gate)) {
            return new ResponseBean(200, 1, "添加成功", "添加成功");
        } else {
            return new ResponseBean(500, 1, "添加失败", "添加失败");
        }
    }


    /**
     * 修改闸机信息
     */
    @RequestMapping("/updateGate")
    public ResponseBean updateGate(@RequestBody Gate gate , @RequestParam(required = false) Integer type) {
        if (gateService.updateById(gate)) {
            return new ResponseBean(0, 1, "修改成功", "修改成功");
        } else {
            return new ResponseBean(500, 1, "修改失败", "修改失败");
        }
    }

    /**
     * 获取闸机进出信息（刷卡记录）
     *
     * @return
     */
    @RequestMapping("/getRecordsByPage")
    public ResponseBean getRecordsByPage(String gateId, String cardNo , String uname, @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit) {
        if (ValidateHelper.isEmptyString(cardNo)) {
            cardNo = null;
        }
        if (ValidateHelper.isEmptyString(uname)) {
            uname = null;
        }
        IPage iPage = gateReaderService.getByPager(gateId,cardNo,uname, page, limit);
        return new ResponseBean(0, iPage.getTotal(), "成功", iPage.getRecords());
    }

}
